package third.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ApplicationServlet", value = "/application")
public class ApplicationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1获取ServletContext对象 （application)
        //第一种方式: this.getServletContext();
        ServletContext application = this.getServletContext();
        System.out.println("application:" + application.hashCode());
        application.setAttribute("appname", "人人网");
        application.getAttribute("appname");
        application.removeAttribute("appname");
        //第二种方式:request.getServletContext();
        ServletContext application2 = request.getServletContext();
        System.out.println("application2:" + application2.hashCode());
        //获取路径（下载文件）
        String realPath = application.getRealPath("/");
        System.out.println(realPath);
        //获取上下文路径(使用跟路径)
        String contextPath = application.getContextPath();
        System.out.println(contextPath);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
